package com.sunday.android.fangdaifu.bukong.utils.noticeview;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.activity.WebViewActivity;
import com.sunday.android.fangdaifu.bukong.entity.IndexTop;
import com.sunday.android.fangdaifu.bukong.utils.Constants;

import java.util.List;

public class NoticeAdapter {
    private List<IndexTop.ListBean> mDatas;

    public NoticeAdapter(List<IndexTop.ListBean> datas) {
        this.mDatas = datas;
        if (datas == null || datas.isEmpty()) {
            throw new RuntimeException("nothing to show");
        }
    }

    /**
     * 获取数据的条数
     *
     * @return
     */
    public int getCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    /**
     * 获取摸个数据
     *
     * @param position
     * @return
     */
    public IndexTop.ListBean getItem(int position) {
        return mDatas.get(position);
    }

    /**
     * 获取条目布局
     *
     * @param parent
     * @return
     */
    public View getView(NoticeView parent) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.notice_item, null);
    }

    /**
     * 条目数据适配
     *
     * @param view
     * @param data
     */
    public void setItem(final View view, final IndexTop.ListBean data) {
        final TextView tv = (TextView) view.findViewById(R.id.title);
        tv.setText(data.getTitle());
        // 你可以增加点击事件
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 比如打开url
                if (data == null) {
                    return;
                }
                Intent intent = new Intent(view.getContext(), WebViewActivity.class);
                intent.putExtra(Constants.TITLE,Constants.NEW_TITLE);
                intent.putExtra(Constants.URL,data.getContext());
                view.getContext().startActivity(intent);
            }
        });
    }
}